package com.zretc.recommend.service.impl;

import com.zretc.api.pojo.SysUser;
import com.zretc.api.pojo.recommendModule.Applications;
import com.zretc.api.pojo.recommendModule.Recommend;
import com.zretc.api.pojo.recommendModule.applicationDTO;
import com.zretc.common.constant.SecurityConstant;
import com.zretc.common.constant.WebConstant;
import com.zretc.common.entity.Pager;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import com.zretc.recommend.dao.applicationMapper;
import com.zretc.recommend.dao.recommendMapper;
import com.zretc.recommend.service.FileServiceClient;
import com.zretc.recommend.service.recommendService;
import com.zretc.redis.service.RedisService;
import com.zretc.security.entity.LoginUser;
import javafx.application.Application;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.rmi.CORBA.Util;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class recommendServiceimpl implements recommendService {

    @Autowired
    private recommendMapper recommendMapper;
    @Autowired
    private applicationMapper applicationMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    private FileServiceClient fileServiceClient;
    @Override
    public Map<String, Object> getRecommendById(Integer pageNum, Integer pageSize,String cityname,String keywords) {
        String cacheKey = "recommend:" + cityname + ":" + keywords+ ":" +pageNum+ ":" +pageSize;//构造键
        Map<String, Object> cachedData = redisService.getCacheMap(cacheKey);
        if (cachedData != null&&!cachedData.isEmpty()) {
            return cachedData; // 缓存命中，直接返回缓存数据
        }
        Pager pager = new Pager(recommendMapper.getCount(), pageNum, pageSize);
        ArrayList<Recommend> recommends = recommendMapper.queryByPage(pager.getStartIndex(), pager.getPageSize(),cityname,keywords);
        HashMap<String,Object> map = new HashMap<>();
        map.put("pager",pager);
        map.put("list",recommends);
        redisService.setCacheMap(cacheKey,map);
        return map;
    }

    @Override
    public Object getMsgById(Integer id) {

        return recommendMapper.getMsgById(id);
    }

    @Override
    public RespResult addRecommend(Recommend recommend) {

        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        recommend.setRecommender_id(loginUser.getUser().getId());
        if(recommendMapper.addRecommend(recommend)){
            return R.success("添加成功");
        }
        return R.fail(WebConstant.ADD_ERROR_CODE,WebConstant.ERROR_504);

    }

    @Override
    public RespResult applicate(Integer id, Applications applications, MultipartFile file) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        applications.setRecommend_id(id);
        applications.setRecommended_id(loginUser.getUser().getId());
        applications.setRecommended_name(loginUser.getUser().getUser_name());
//        Applications applications = new Applications();
//        BeanUtils.copyProperties(applicationDTO,applications);
        try {
            file.getInputStream().close();
            //inputStream.close()
            System.gc();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        String url = fileServiceClient.upload(file).getData().toString();
        applications.setFile_url(url);
        if(applicationMapper.addappliate(applications)){
            return R.success("申请成功");
        }
        return R.fail(WebConstant.ADD_ERROR_CODE,WebConstant.ERROR_504);

    }

    @Override
    public Object aaa(MultipartFile[] file) {
        for(final MultipartFile file1 : file) {
            try {
                file1.getInputStream().close();
                //inputStream.close()
                System.gc();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            String url = fileServiceClient.upload(file1).getData().toString();
            System.out.println(url);
        }
        return null;
    }

    @Override
    public RespResult test() {
        for(int i=0;i<20;i++){
            Recommend recommend = new Recommend();
            recommend.setRecommender_id(i);
            recommend.setJob_name("测试数据"+i);
            recommend.setCompany_name("测试数据"+i);
            recommendMapper.addRecommend(recommend);
        }
        return null;
    }


}
